package com.share.lagou.pipeline;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * pipeline编排层
 * @author caifan
 * @since 2022/7/18
 */
public class PipelineServerDemo01 {

    public static void main(String[] args) throws Exception {
        final NioEventLoopGroup boss = new NioEventLoopGroup();
        final NioEventLoopGroup worker = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        try {
            serverBootstrap.group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            // inBound
                            ch.pipeline().addLast(new SampleInBoundHandler("SampleInboundHandlerA", false))
                                    .addLast(new SampleInBoundHandler("SampleInboundHanderB", false))
                                    .addLast(new SampleInBoundHandler("SampleInboundHanderC", true));

                            // outBound
                            ch.pipeline().addLast(new SampleOutBoundHandler("SampleOutboundHandlerA"))
                                    .addLast(new SampleOutBoundHandler("SampleOutboundHandlerB"))
                                    .addLast(new SampleOutBoundHandler("SampleOutboundHandlerC"))
                                    .addLast(new ExceptionHandler());
                        }
                    });
            final ChannelFuture channelFuture = serverBootstrap.bind(7777).sync();
            System.out.println("server starting...");
            channelFuture.channel().closeFuture();
        } finally {
//            boss.shutdownGracefully();
//            worker.shutdownGracefully();
        }
    }
}
